package com.spdb.spdbfl.restful.lease;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spdb.spdbfl.config.BaseConfig;
import com.spdb.spdbfl.utils.ConfigProperty;
import com.spdb.spdbfl.utils.GetBaseDataUtil;
import com.spdb.spdbfl.utils.HttpRequest;
import com.spdb.spdbfl.utils.CommonUtil;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


@Test(description = "租赁物")
public class LeaseIT {

    public static final String operationCenterUrl = "/operationCenter/operationCenter";
    static String domainUrl = "";
    static String token = "";
    static String url = "";
    static String leaseItemId = "";
    static String leaseAssetsId = "";

    @BeforeClass
    public void setUp(){
        domainUrl = BaseConfig.domainUrl;
        url = domainUrl + operationCenterUrl;
        System.out.println("url:"+url);
        token = ConfigProperty.getProperty("liumToken");
    }


    @Test(description = "获取token，拼接url", priority = 0)
    public static void getTokenAndUrl() throws Exception {
        domainUrl = BaseConfig.domainUrl;
        url = domainUrl + operationCenterUrl;
        token = ConfigProperty.getProperty("liumToken");
    }

    @Test(description = "新增/更新租赁物汇总信息", priority = 0)
    public static void updateLeaseItem() {
        String param = updateLeaseItemParam();
        HttpRequest httpRequest = new HttpRequest();
        String result = httpRequest.httpPost(url, token, param);

        JSONObject jsonResult = JSONObject.parseObject(result);
        Assert.assertEquals(jsonResult.get("code"), "200");
        Assert.assertEquals(jsonResult.get("msg").toString(), "succ");

        leaseItemId = jsonResult.get("tableid").toString();
        System.out.println("leaseItemId:" + leaseItemId);
        String itemName = GetBaseDataUtil.getItemName(leaseItemId);
        ConfigProperty.updateProperty("leaseItemId", leaseItemId);
        ConfigProperty.updateProperty("itemName", itemName);
    }

    @Test(description = "新增/更新资产", priority = 1)
    public static void updateLeaseAssets() {
        String param = updateLeaseAssetsParam();
        HttpRequest httpRequest = new HttpRequest();
        String result = httpRequest.httpPost(url, token, param);

        JSONObject jsonResult = JSONObject.parseObject(result);
        Assert.assertEquals(jsonResult.get("code"), "200");
        Assert.assertEquals(jsonResult.get("msg").toString(), "succ");

        String tableId = jsonResult.get("tableid").toString();
        System.out.println("leaseAssetsId:" + tableId);
        leaseAssetsId = tableId;
//        return tableId;
    }

    @Test(description = "新增/更新账面价值", priority = 2)
    public static void updateAssetsValue() {
        String param = updateAssetsValueParam();
        HttpRequest httpRequest = new HttpRequest();
        String result = httpRequest.httpPost(url, token, param);

        JSONObject jsonResult = JSONObject.parseObject(result);
        Assert.assertEquals(jsonResult.get("code"), "200");
        Assert.assertEquals(jsonResult.get("msg").toString(), "succ");
        String tableId = jsonResult.get("tableid").toString();
        System.out.println("tableid:" + tableId);
    }

    @Test(description = "新增/添加资产分类", priority = 3)
    public static void addLeaseObject() {
        String param = addLeaseObjectParam();
        HttpRequest httpRequest = new HttpRequest();
        String result = httpRequest.httpPost(url, token, param);

        JSONObject jsonResult = JSONObject.parseObject(result);
        Assert.assertEquals(jsonResult.get("code"), "200");
        Assert.assertEquals(jsonResult.get("msg").toString(), "succ");
    }

    @Test(description = "新增/更新出厂信息", priority = 4)
    public static void updateAssetsFactory() {
        String param = updateAssetsFactoryParam();
        HttpRequest httpRequest = new HttpRequest();
        String result = httpRequest.httpPost(url, token, param);

        JSONObject jsonResult = JSONObject.parseObject(result);
        Assert.assertEquals(jsonResult.get("code"), "200");
        Assert.assertEquals(jsonResult.get("msg").toString(), "succ");
    }


    @Test(description = "新增/更新维修状况", priority = 5)
    public static void insertAssetsMaintain() {
        String param = insertAssetsMaintainParam();
        HttpRequest httpRequest = new HttpRequest();
        String result = httpRequest.httpPost(url, token, param);

        JSONObject jsonResult = JSONObject.parseObject(result);
        Assert.assertEquals(jsonResult.get("code"), "200");
        Assert.assertEquals(jsonResult.get("msg").toString(), "succ");
    }


    @Test(description = "新增/更新权属及注册", priority = 6)
    public static void updateAssetsOwner() {
        String param = updateAssetsOwnerParam();
        HttpRequest httpRequest = new HttpRequest();
        String result = httpRequest.httpPost(url, token, param);

        JSONObject jsonResult = JSONObject.parseObject(result);
        Assert.assertEquals(jsonResult.get("code"), "200");
        Assert.assertEquals(jsonResult.get("msg").toString(), "succ");
    }

    @Test(description = "新增/更新使用地点", priority = 7)
    public static void updateAssetsPlace() {
        String param = updateAssetsPlaceParam();
        HttpRequest httpRequest = new HttpRequest();
        String result = httpRequest.httpPost(url, token, param);

        JSONObject jsonResult = JSONObject.parseObject(result);
        Assert.assertEquals(jsonResult.get("code"), "200");
        Assert.assertEquals(jsonResult.get("msg").toString(), "succ");
    }

    /**
     * 构建insertAssetsMaintain入参
     *
     * @return
     */
    private static String insertAssetsMaintainParam() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("createperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject.put("isDevalue", "1");
        jsonObject.put("isPerfect", "1");
        jsonObject.put("isRealCheck", "1");
        jsonObject.put("isUseCust", "1");
        jsonObject.put("lastMaintainTime", "2022-09-02");
        jsonObject.put("leaseAssetsId", leaseAssetsId);
        jsonObject.put("maintainPeriod", "2");
        jsonObject.put("modifyperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject.put("nextMaintainTime", "2024-09-02");
        jsonObject.put("remark", "良好");
        jsonObject.put("thisDepreciation", 100000);
        jsonObject.put("useIsMaintain", "1");

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("param", jsonObject);
        jsonObject1.put("className", "LeaseItemService");
        jsonObject1.put("method", "insertAssetsMaintain");
        return jsonObject1.toJSONString();
    }

    /**
     * 构建updateAssetsPlace入参
     *
     * @return
     */
    private static String updateAssetsPlaceParam() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("address", "上海市闵行区");
        jsonObject.put("createperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject.put("leaseAssetsId", leaseAssetsId);
        jsonObject.put("modifyperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject.put("nation", "CHN");
        jsonObject.put("site", "310000");

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("param", jsonObject);
        jsonObject1.put("className", "LeaseItemService");
        jsonObject1.put("method", "updateAssetsPlace");
        return jsonObject1.toJSONString();
    }


    /**
     * 构建updateAssetsFactory入参
     *
     * @return
     */
    private static String updateAssetsFactoryParam() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("createperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject.put("factoryCountry", "CHN");
        jsonObject.put("factoryDate", "2022-09-01");
        jsonObject.put("factoryPeriod", "2021-09-01");
        jsonObject.put("factoryPeriodEnd", "2022-09-01");
        jsonObject.put("factorySN", CommonUtil.getRandomName("CCXLH"));
        jsonObject.put("factorySite", "上海");
        jsonObject.put("factoryname", "测试制造商名称");
        jsonObject.put("leaseAssetsId", leaseAssetsId);
        jsonObject.put("modifyperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject.put("usedPeriod", "2022-09-01");
        jsonObject.put("usedPeriodEnd", "2032-09-01");

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("param", jsonObject);
        jsonObject1.put("className", "LeaseItemService");
        jsonObject1.put("method", "updateAssetsFactory");
        return jsonObject1.toJSONString();
    }

    /**
     * 构建updateAssetsOwner入参
     *
     * @return
     */
    private static String updateAssetsOwnerParam() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("clearWay", "05");
        jsonObject.put("createperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject.put("isMonopolisticMarket", "1");
        jsonObject.put("isQsReg", "1");
        jsonObject.put("isRipe", "1");
        jsonObject.put("leaseAssetsId", leaseAssetsId);
        jsonObject.put("modifyperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject.put("ownDate", "2022-09-07");
        jsonObject.put("registerNo", CommonUtil.getRandomName("DJHM"));
        jsonObject.put("registerOrg", CommonUtil.getRandomName("DJJG"));
        jsonObject.put("registerType", "01");
        jsonObject.put("remarks", "说明测试");

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("className", "LeaseItemService");
        jsonObject1.put("method", "updateAssetsOwner");
        jsonObject1.put("param", jsonObject);
        return jsonObject1.toJSONString();
    }


    /**
     * 构建addLeaseObject入参
     *
     * @return
     */
    private static String addLeaseObjectParam() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("assetsTypeNew", "1-1.04");
        jsonObject.put("bookValue", 1000000000);
        jsonObject.put("evalValue", 1000000000);
        jsonObject.put("netValue", 1000000000);
        jsonObject.put("purchasePrice", 1000000000);
        jsonObject.put("valueRatio", "1.0000");

        JSONArray objects = new JSONArray();
        objects.add(jsonObject);

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("createperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject1.put("leaseAssetsId", leaseAssetsId);
        jsonObject1.put("leaseObject", objects);
        jsonObject1.put("modifyperson", "8af4e52656c4af8a0156c59ba4f30047");

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("className", "leaseObject");
        jsonObject2.put("method", "addLeaseObject");
        jsonObject2.put("param", jsonObject1);
        return jsonObject2.toJSONString();
    }

    /**
     * 构建updateAssetsValue入参
     *
     * @return
     */
    private static String updateAssetsValueParam() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("comprehensiveRate", 0.05);
        jsonObject.put("createperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject.put("modifyperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject.put("evalMethod", "06");
        jsonObject.put("evalRate", "01");
        jsonObject.put("evalValue", 1000000000);
        jsonObject.put("initialValueMethod", "3");
        jsonObject.put("leaseAssetsId", leaseAssetsId);
        jsonObject.put("netValue", 1000000000);
        jsonObject.put("usefulLife", "10");
        jsonObject.put("valuationFrequency", "01");
        jsonObject.put("valuationMethod", "06");

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("param", jsonObject);
        jsonObject1.put("className", "LeaseItemService");
        jsonObject1.put("method", "updateAssetsValue");
        return jsonObject1.toJSONString();
    }


    /**
     * 构建updateLeaseItem入参
     *
     * @return
     */
    public static String updateLeaseItemParam() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("createperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject.put("currency", "CNY");
        jsonObject.put("itemName", CommonUtil.getRandomName("itemName"));
        jsonObject.put("modifyperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject.put("remarks", "这是租赁物描述");
        jsonObject.put("tableid", "");
        jsonObject.put("totalPrice", 1000000000);

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("param", jsonObject);
        jsonObject1.put("className", "LeaseItemService");
        jsonObject1.put("method", "updateLeaseItem");

        return jsonObject1.toJSONString();
    }


    /**
     * 构建updateLeaseAssets入参
     *
     * @return
     */
    public static String updateLeaseAssetsParam() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("amount", 1);
        jsonObject.put("assetsName", CommonUtil.getRandomName("飞机"));
        jsonObject.put("assetsType", "C-17-171");
        jsonObject.put("assetsTypeNew", "2-2.15-2.15.01-2.15.01.03");
        jsonObject.put("createperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject.put("deptrciationReason", "");
        jsonObject.put("isDepreciation", "1");
        jsonObject.put("isExtension", "1");
        jsonObject.put("isFlaw", "0");
        jsonObject.put("isNetRegister", "1");
        jsonObject.put("leaseItemId", leaseItemId);
        jsonObject.put("modifyperson", "8af4e52656c4af8a0156c59ba4f30047");
        jsonObject.put("noAssetsOrg", "1");
        jsonObject.put("othersReason", "");
        jsonObject.put("purchasePrice", 1000000000);
        jsonObject.put("registerDate", CommonUtil.getCurrentTime());
        jsonObject.put("registerNo", CommonUtil.getRandomName("DJBH"));
        jsonObject.put("sts", "03");
        jsonObject.put("unit", "元");
        jsonObject.put("tableid", "");

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("param", jsonObject);
        jsonObject1.put("className", "LeaseItemService");
        jsonObject1.put("method", "updateLeaseAssets");
        return jsonObject1.toJSONString();
    }


    public static void main(String[] args) {
        updateLeaseItem();
    }
}
